package com.wsz.educenter.controller;

import com.wsz.commonutils.JwtUtils;
import com.wsz.commonutils.R;
import com.wsz.commonutils.ordervo.MemberUtils;
import com.wsz.educenter.pojo.Member;
import com.wsz.educenter.pojo.vo.RegisterVo;
import com.wsz.educenter.service.MemberService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/educenter/ucenter")
public class CenterController {
    @Autowired
    private MemberService memberService;

    @PostMapping("/login")
    public R login(@RequestBody Member member){
        String token = memberService.login(member);
        return R.ok().data("token",token);
    }

    @PostMapping("/register")
    public R register(@RequestBody RegisterVo registerVo) {

        memberService.register(registerVo);

        return R.ok();
    }

    @GetMapping("/getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){

        String memberId = JwtUtils.getMemberIdByJwtToken(request);

        Member member = memberService.getById(memberId);

        return R.ok().data("userInfo",member);
    }

    @PostMapping("/getMemberInfoOrder/{id}")
    public MemberUtils getMemberInfoOrder(@PathVariable String id){
        Member member = memberService.getById(id);
        MemberUtils memberUtils = new MemberUtils();
        BeanUtils.copyProperties(member,memberUtils);
        return memberUtils;
    }

    @ApiOperation("查询某一天注册人数")
    @GetMapping("/countRegister/{day}")
    public R countRegister(@PathVariable String day) {

        Integer count = memberService.countRegister(day);

        return R.ok().data("countRegister",count);
    }

    @ApiOperation("通过Id查寻用户信息")
    @GetMapping("/getMember/{id}")
    public R getMember(@PathVariable String id){

        Member member = memberService.getById(id);

        return R.ok().data("member",member);
    }

    @ApiOperation("通过Id修改用户信息")
    @PostMapping("/updateMember")
    public R updateMember(@RequestBody Member member){
        boolean b = memberService.updateById(member);
        if(b) return R.ok();
        else return R.error();
    }

}
